//
//  SaveManager.m
//  AiYuanTai
//
//  Created by cdyqkj on 16/6/23.
//  Copyright © 2016年 sima. All rights reserved.
//

#import "SaveManager.h"

@implementation SaveManager
static  SaveManager *saveManager;
+ (instancetype)shareSaveManager
{
    @synchronized(self) {
        if (saveManager == nil) {
            saveManager = [[SaveManager alloc] init]; // assignment not done here
        }
    }
    return saveManager;
}
/**
 *  account : 0,
	nickName : 18000534097,
	vipFrom : 1,
	integration : 500,
	updateTime : 2015-05-14 11:48:20,
	sex : 2,
	waitReadCount : 0,
	uuid : 18000534097,
	mobilePhone : 18000534097,
	waitPay : 0,
	waitSend : 0,
	waitComment : 0,
	vipToken : <null>,
	id : 1,
	email : <null>,
	passWord : 202cb962ac59075b964b07152d234b70,
	waitGet : 0,
	loginName : 18000534097,
	createTime : 2015-05-14 11:48:20,
	photoUrl : <null>

 */
- (void)saveUserInfo:(NSDictionary*)dict {
    // 获取NSUserDefaults对象
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    // 保存数据(如果设置数据之后没有同步, 会在将来某一时间点自动将数据保存到Preferences文件夹下面)
    [defaults setObject:dict forKey:@"user_info"];
//    [defaults setObject:@"tinghou" forKey:@"name"];
//    [defaults setObject:@"man" forKey:@"gender"];
//    [defaults setInteger:13 forKey:@"age"];
//    [defaults setDouble:10.1 forKey:@"height"];
    
    
    // 让NSUserDefaults立刻保存数据 注意:UserDefaults设置数据时，不是立即写入，而是根据时间戳定时地把缓存中的数据写入本地磁盘。所以调用了set方法之后数据有可能还没有写入磁盘应用程序就终止了。出现以上问题，可以通过调用synchornize方法强制写入
    
    [defaults synchronize];  //如果要立刻保存就需要这行代码
//    // 1.获取NSUserDefaults
//    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//    // 2.通过NSUserDefaults获取保存的数据
//    NSString *name = [defaults objectForKey:@"name"];
//    int age = [defaults integerForKey:@"age"];
//    [[EncryptionUserDefaults standardUserDefaults] setObject:dict forKey:@"user_info"];
    
}

- (NSDictionary*)getUserInfo {
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_info"];
    return dict;
}

- (NSString *)getUserName
{
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_info"];
    if ([dict[@"name"] isEqualToString:@""]) {
        return dict[@"mobile"];
    } else {
        return dict[@"name"];
    }
}
- (void)updateUserName:(NSString *)str
{
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_info"];
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithDictionary:dict];
    dict1[@"userName"] = str;
    [[NSUserDefaults standardUserDefaults] setObject:dict1 forKey:@"user_info"];
}
- (NSString *)getUserEmail
{
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_info"];
    
    if ([[dict[@"email"] class] isSubclassOfClass:NSClassFromString(@"NSNull")]) {
         return  @"未设置";
    } else {
        return dict[@"email"];
    }
    
}
- (void)updateEmail:(NSString *)str
{
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_info"];
    [dict setValue:str forKey:@"email"];
    [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"user_info"];
}
- (NSData *)getUserPhotoUrl
{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"photoUrl"];
    
    return data;

}
-(void)updatePhotoUrl:(NSData *)data
{
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"photoUrl"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSString *)getSex
{
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_info"];
    NSString *str = nil;
    if ([dict[@"sex"] integerValue] == 0) {
        str = @"女";
    } else if ([dict[@"sex"] integerValue] == 1) {
        str = @"男";
    } else {
        str = @"保密";
    }
    return str;
}
-(void)updateSex:(NSString *)str
{
    NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_info"];
    if ([str  isEqual: @"女"]) {
        [dict setValue:@0 forKey:@"sex"];
    } else if ([str  isEqual: @"男"]) {
        [dict setValue:@1 forKey:@"sex"];
    } else {
        [dict setValue:@2 forKey:@"sex"];

    }
    [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"user_info"];
}
-(void)saveSeachHistroy:(NSString *)str
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSArray *dict = [defaults objectForKey:@"history"];
    
    NSMutableArray *arr = [NSMutableArray arrayWithArray:dict ];
    if (![arr containsObject:str]) {
        
        [arr addObject:str];
    }
    [defaults setObject:arr forKey:@"history"];
    [defaults synchronize];
}
- (NSArray *)getSeachHistroy
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:@"history"];
}
- (void)cleanHistory
{
    NSMutableArray *arr = [NSMutableArray array];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:arr forKey:@"history"];
    [defaults synchronize];
}
@end
